package com.weimengchao.task.domain;

import org.springframework.util.StringUtils;

public enum ExcelType {

    UnKnown(0, "未知"),
    CSV(1, "csv"),
    XLS(2, "xls"),
    XLSX(3, "xlsx"),
    ;

    private final Integer index;
    private final String suffix;

    ExcelType(Integer index, String suffix) {
        this.index = index;
        this.suffix = suffix;
    }

    public static ExcelType toExcelType(String suffix) {
        if (StringUtils.isEmpty(suffix)) {
            return UnKnown;
        }
        switch (suffix) {
            case "csv":
                return CSV;
            case "xls":
                return XLS;
            case "xlsx":
                return XLSX;
            default:
                return UnKnown;
        }
    }

    public static ExcelType valueOf(int index) {
        switch (index) {
            case 1:
                return CSV;
            case 2:
                return XLS;
            case 3:
                return XLSX;
            default:
                return UnKnown;
        }
    }

    public static ExcelType getExcelType(String path) {
        return ExcelType.toExcelType(path.toLowerCase());
    }

    //获取后缀
    public static String getSuffix(String path) {
        if (StringUtils.isEmpty(path)) {
            return "";
        }
        int index = path.lastIndexOf(".");
        if (index == -1) {
            return "";
        }
        return path.substring(index + 1);
    }

    public Integer getIndex() {
        return index;
    }

    public String getSuffix() {
        return suffix;
    }

}
